package com.mebs.model.logistics;

import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;
import static org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy;

import javax.persistence.*;
import java.util.Date;

import com.mebs.model.AuditInfo;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Feb 13, 2006, 4:54:26 PM
 */
@Entity()
@Table(name = "LEG",
		uniqueConstraints = {@UniqueConstraint(columnNames={"TransportationId", "IndexNumber"})}
)
public class Leg {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Long id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "TransportationId")
    @Field(type = ArgumentType.INSTANCE)
    private Transportation transportation;

    @Column(name = "ETA", nullable = true)
    private Date eta;

    @Column(name = "ETD", nullable = true)
    private Date etd;

    @Column(name = "ATA", nullable = true)
    private Date ata;

    @Column(name = "ATD", nullable = true)
    private Date atd;

    @Column(name = "Comments", nullable = true, length=10000)
    private String comments;

    @Column(name = "Number", nullable = true, length = 250)
    private String number;

    @Column(name = "IsSplit", nullable = true)
    private Boolean split;
    
    @Column(name = "lastLeg", nullable = true)
    private Boolean lastLeg;

    //@ManyToOne(optional = false)
    //@JoinColumn(name = "OriginId")
    @Column(name = "Origin", nullable = true, length = 250)
    private String origin;

    //@ManyToOne(optional = false)
    //@JoinColumn(name = "DestinationId")
    @Column(name = "Destination", nullable = true, length = 250)
    private String destination;

    @OneToOne(optional = true)
    @JoinColumn(name = "LegId")
    @Field(type = ArgumentType.INSTANCE)
    private LogisticsOperation legOperation;

    @Embedded
    private AuditInfo auditInfo;
    
    @Column(name="IndexNumber", nullable = false)
    private Integer indexNumber;
    
    @Column(name = "Flight_vessel_number", nullable = false, length = 30)
    private String flightVesselNumber;

    /**
     * ********************** Getters-setters ***********************
     */
    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public Transportation getTransportation()
    {
        return transportation;
    }

    public void setTransportation(Transportation transportation)
    {
        this.transportation = transportation;
    }

    public Date getEta()
    {
        return eta;
    }

    public void setEta(Date eta)
    {
        this.eta = eta;
    }

    public Date getEtd()
    {
        return etd;
    }

    public void setEtd(Date etd)
    {
        this.etd = etd;
    }

    public Date getAta()
    {
        return ata;
    }

    public void setAta(Date ata)
    {
        this.ata = ata;
    }

    public Date getAtd()
    {
        return atd;
    }

    public void setAtd(Date atd)
    {
        this.atd = atd;
    }

    public String getComments()
    {
        return comments;
    }

    public void setComments(String comments)
    {
        this.comments = comments;
    }

    public String getNumber()
    {
        return number;
    }

    public void setNumber(String number)
    {
        this.number = number;
    }

    public Boolean getSplit()
    {
        return split;
    }

    public void setSplit(Boolean split)
    {
        this.split = split;
    }
    
    public Boolean getLastLeg()
    {
        return lastLeg;
    }

    public void setLastLeg(Boolean lastLeg)
    {
        this.lastLeg = lastLeg;
    }

    public String getOrigin()
    {
        return origin;
    }

    public void setOrigin(String origin)
    {
        this.origin = origin;
    }

    public String getDestination()
    {
        return destination;
    }

    public void setDestination(String destination)
    {
        this.destination = destination;
    }

    public LogisticsOperation getLegOperation()
    {
        return legOperation;
    }

    public void setLegOperation(LogisticsOperation legOperation)
    {
        this.legOperation = legOperation;
    }

    public AuditInfo getAuditInfo()
    {
        if (this.auditInfo == null) {
            this.auditInfo = new AuditInfo();
        }
        return auditInfo;
    }

    public void setAuditInfo(AuditInfo auditInfo)
    {
        this.auditInfo = auditInfo;
    }

	public String getFlightVesselNumber() {
		return flightVesselNumber;
	}

	public void setFlightVesselNumber(String flightVesselNumber) {
		this.flightVesselNumber = flightVesselNumber;
	}

	@Override
    public String toString()
    {
        return this.comments;
    }

    @Transient
    public boolean isDeletable()
    {
        return this.legOperation == null;
    }

    public Integer getIndexNumber() {
		return indexNumber;
	}

	public void setIndexNumber(Integer indexNumber) {
		this.indexNumber = indexNumber;
	}

	@Override
    public int hashCode()
    {
        if (id == null) return super.hashCode();
        return id.hashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null) return false;
        if (this == o) return true;
        if (id == null) return false;
        if (getClassWithoutInitializingProxy(this) != getClassWithoutInitializingProxy(o))
            return false;
        final Leg that = (Leg) o;
        return id.equals(that.getId());
    }
}
